const {ipcRenderer} = require('electron');
const $ = require('./resources/jquery3-7-0.mini')

//获取鼠标按下时的x位置
let getWindowBeforePageX;
//获取鼠标松开时的x位置
let getWindowafterPageX;
//获取鼠标按下时的y位置
let getWindowBeforePageY;
//获取鼠标松开时的y位置
let getWindowafterPageY;

let setMessage = []
let setImage = [
    './images/leizhenyu.svg',
    './images/zhongddayu.svg',
    './images/duoyun_white.svg',
    './images/xiaoyu_white.svg'
]
window.addEventListener('DOMContentLoaded',async()=>{
    $.ajax({
        url: 'https://v0.yiketianqi.com/api?unescape=1&version=v91&appid=89555146&appsecret=pjuSyqB2&ext=&cityid=&city=',
        type: 'get',
        data: {
            
        },
        success: function (res) {
            // console.log(res.city);
            $('.messageBox .city').text(res.city)
            $('.messageBox .wea').text(res.data[0].wea)
            // $('.messageBox1 .city').text(res.city)
            // $('.messageBox1 .wea').text(res.data[1].wea)
            setMessage[0] = {city:res.city,wea:res.data[0].wea}
            setMessage[1] = {city:res.city,wea:res.data[1].wea}
            //初始化图标
            switch(res.data[0].wea){
                case "雷阵雨":
                    $('.tianqiImage').attr('src',setImage[0])
                    $('body').css('background','url("./images/leizhenyu.gif")')
                    $('.showDay').css('color','#fff')
                    $('.city').css('color','#fff')
                    $('.wea').css('color','#fff')
                    break
                case "中到大雨转雷阵雨":
                    $('.tianqiImage').attr('src',setImage[0])
                    break
                case "多云":
                case "雷阵雨转多云":
                    $('.tianqiImage').attr('src',setImage[2])
                    $('body').css('background','url("./images/duoyun.gif")')
                    $('.showDay').css('color','#fff')
                    $('.city').css('color','#fff')
                    $('.wea').css('color','#fff')
                    break
                case "小雨":
                case "小到中雨转小雨":
                    $('.tianqiImage').attr('src',setImage[3])
                    $('body').css('background','url("./images/xiaoyu.gif")')
                    $('.showDay').css('color','#fff')
                    $('.city').css('color','#fff')
                    $('.wea').css('color','#fff')
                    break
            }
            
        }
    });
});

let i = 0
const change = async() => {
    const pinnedImage = document.getElementById("pinnedImage");
    console.log(pinnedImage.src,i)
    if(i == 0){
        pinnedImage.style.transform = 'rotate3d(0,0,1,-45deg)';
        ipcRenderer.send('tianqi-top')
        i = 1;
    }else{
        // pinnedImage.src = "images/Pinned_xie.svg"
        pinnedImage.style.transform = 'rotate3d(0,0,1,0deg)';
        ipcRenderer.send('tianqi-no-top')
        i = 0;
    }
}

const clickClose = async() => {
    ipcRenderer.send('tianqi-close');
}

window.addEventListener('mousedown',(event)=>{
    if(event.pageX || event.pageY){
        getWindowBeforePageX = event.pageX
        getWindowBeforePageY = event.pageY
        console.log({x:event.pageX,y:event.pageY})
    }
})
//鼠标松开时触发
window.addEventListener('mouseup',(event)=>{
    if(event.pageX || event.pageY){
        getWindowafterPageX = event.pageX
        getWindowafterPageY = event.pageY
        console.log({x:event.pageX,y:event.pageY})
    }
    if(getWindowafterPageX - getWindowBeforePageX > 0){
        //左拉
        console.log('右拉')
        ipcRenderer.send('show-newWin-right')
    }
})

let tqText = ['今日','明日']
let set = 0
window.onmousewheel = async(event) => {
    console.log(setMessage[0].wea)
    
    if(event.wheelDelta < 0){
        console.log('<0')
        //向下
        i=1
        $('.showDay').text(tqText[i])
        $('.city').text(setMessage[i].city)
        $('.wea').text(setMessage[i].wea)
        switch(setMessage[i].wea){
            case "中到大雨转雷阵雨":
                $('.tianqiImage').attr('src',setImage[0])
                break
            case "多云":
            case "雷阵雨转多云":
                $('.tianqiImage').attr('src',setImage[2])
                $('body').css('background','url("./images/duoyun.gif")')
                $('.showDay').css('color','#fff')
                $('.city').css('color','#fff')
                $('.wea').css('color','#fff')
                break
            case "小雨":
            case "小到中雨转小雨":
                $('.tianqiImage').attr('src',setImage[3])
                $('body').css('background','url("./images/xiaoyu.gif")')
                $('.showDay').css('color','#fff')
                $('.city').css('color','#fff')
                $('.wea').css('color','#fff')
                break
        }
    }
    if(event.wheelDelta > 0){
        // console.log('>0')
        //向上
        i=0
        $('.showDay').text(tqText[i])
        $('.city').text(setMessage[i].city)
        $('.wea').text(setMessage[i].wea)
        switch(setMessage[i].wea){
            case "雷阵雨":
                $('.tianqiImage').attr('src',setImage[0])
                break
            case "中到大雨转雷阵雨":
                $('.tianqiImage').attr('src',setImage[0])
                break
                case "雷阵雨转多云":
                    $('.tianqiImage').attr('src',setImage[2])
                    $('body').css('background','url("./images/duoyun.gif")')
                    $('.showDay').css('color','#fff')
                    $('.city').css('color','#fff')
                    $('.wea').css('color','#fff')
                    break
                case "小雨":
                case "小到中雨转小雨":
                    $('.tianqiImage').attr('src',setImage[3])
                    $('body').css('background','url("./images/xiaoyu.gif")')
                    $('.showDay').css('color','#fff')
                    $('.city').css('color','#fff')
                    $('.wea').css('color','#fff')
                    break
        }
    }
}